﻿using Apps.Common;
using Apps.Models;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace ReportViewerForMvc
{
    /// <summary>
    /// The Web Form used for rendering a ReportViewer control.
    /// </summary>
    public partial class HCCaculateReportWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ReportViewer1.Drillthrough += new DrillthroughEventHandler(ReportViewer1_Drillthrough);
            BuildReportViewer();
        }

        private void BuildReportViewer()
        {
            if (!IsPostBack)
            {
                ReportViewerForMvc.ReportViewer.ID = ReportViewer1.ID;

                ReportViewer1.SetProperties(ReportViewerForMvc.ReportViewer);
            }
        }

        protected void ReportViewer1_Drillthrough(object sender, DrillthroughEventArgs e)
        {
            LocalReport localReport = (LocalReport)e.Report;
            IList<ReportParameter> list = localReport.OriginalParametersToDrillthrough;
            var whereLambda = PredicateBuilder.True<V_HC_WEIGHING>();
            var RpSTIME = list.Where(m=>m.Name== "RpSTIME").FirstOrDefault().Values[0].ToString();
            var RpETIME = list.Where(m=>m.Name== "RpETIME").FirstOrDefault().Values[0].ToString();
            var CODE = list.Where(m => m.Name == "CODE").FirstOrDefault().Values[0].ToString();
            List<V_HC_WEIGHING> Data = new List<V_HC_WEIGHING>();

            if (!string.IsNullOrWhiteSpace(RpSTIME))
            {
                DateTime FDATETime = Convert.ToDateTime(RpSTIME).Date;
                whereLambda = whereLambda.And<V_HC_WEIGHING>(m => m.TIME >= FDATETime);
            }
            if (!string.IsNullOrWhiteSpace(RpETIME))
            {
                DateTime FDATETime = Convert.ToDateTime(RpETIME).AddDays(1).Date;
                whereLambda = whereLambda.And<V_HC_WEIGHING>(m => m.TIME <= FDATETime);
            }
            whereLambda = whereLambda.And<V_HC_WEIGHING>(m => m.CODE == CODE);

            Data = Apps.BLL.Report.ReportBLL<V_HC_WEIGHING>.GetReportDataList(whereLambda);

            localReport.DataSources.Add(new ReportDataSource { Value = Data, Name = "DataSet1" });

        }

        
    }
}